package com.atguigu.yygh.apartment.controller;

import com.alibaba.excel.util.StringUtils;
import com.atguigu.yygh.apartment.service.ApartmentInfoService;
import com.atguigu.yygh.apartment.service.ApartmentService;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.model.hosp.Apartment;
import com.atguigu.yygh.model.hosp.ApartmentInfoEvVo;
import com.atguigu.yygh.vo.hosp.ApartmentQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "公寓楼设置")
@RestController
@RequestMapping("/admin/apartment/apartmentInfo")
//@CrossOrigin
public class ApartmentController {
    //注入service
    @Autowired
    private ApartmentService apartmentService;
    @Autowired
    private ApartmentInfoService apartmentInfoService;

    //http://localhost:8201/admin/apartment/apartmentInfo/findAll

    //查询all
    @ApiOperation(value = "获取所有公寓信息")
    @GetMapping("findAll")
    public Result findAllApartment(){
        List<Apartment> list=apartmentService.list();
        return Result.ok(list);
    }

    // 逻辑删除
    @ApiOperation(value = "逻辑删除公寓信息")
    @DeleteMapping("{id}")
    public Result removeApartment(@PathVariable Long id){
        boolean flag = apartmentService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }

    //条件查询带分页
    @ApiOperation(value = "条件查询公寓带分页")
    @PostMapping("findPage/{current}/{limit}")
    public Result findPageApartment(@PathVariable long current,
                                    @PathVariable long limit,
                                    @RequestBody(required = false) ApartmentQueryVo apartmentQueryVo){
        //创建page对象,传递当前页与记录数
        Page<Apartment> page=new Page<>(current,limit);
        //构建条件
        QueryWrapper<Apartment> wrapper=new QueryWrapper<>();
        String apartmentName = apartmentQueryVo.getApartmentName();//公寓名称
        String apartmentCode = apartmentQueryVo.getApartmentCode();
        if (!StringUtils.isEmpty(apartmentName)){
            wrapper.like("apartment_name",apartmentQueryVo.getApartmentName());
        }
        if (!StringUtils.isEmpty(apartmentCode)){
            wrapper.eq("apartment_code",apartmentQueryVo.getApartmentCode());
        }
        //调用方法实现分页查询
        Page<Apartment> apartmentPage = apartmentService.page(page, wrapper);
        return Result.ok(apartmentPage);
    }
    //添加公寓设置
    @ApiOperation(value = "添加公寓设置")
    @PostMapping("saveApartment")
    public Result saveApartment(@RequestBody Apartment apartment){
        //设置状态 1可使用 0不能使用
        apartment.setStatus(1);
        //签名秘钥
        Random random = new Random();
        apartment.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        //调用service
        boolean save = apartmentService.save(apartment);
        if (save) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //添加公寓详情
    @ApiOperation(value = "添加公寓详情")
    @PostMapping("saveApartmentInfo")
    public Result saveApartmentInfo(@RequestBody ApartmentInfoEvVo apartmentInfo) throws JsonProcessingException {
        //设置状态 1可使用 0不能使用
        System.out.println(apartmentInfo.toString());
        apartmentInfoService.save(apartmentInfo);
        return Result.ok();
//        apartment.setStatus(1);
//        //签名秘钥
//        Random random = new Random();
//        apartment.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
//        //调用service
//        boolean save = apartmentService.save(apartment);
//        if (save) {
//            return Result.ok();
//        }else {
//            return Result.fail();
//        }
    }
    //根据id获取公寓
    @GetMapping("getApartment/{id}")
    public Result getApartment(@PathVariable Long id){
//        try {
//            //模拟异常
//            //int a=1/0;
//        }catch (Exception e){
//            throw new YyghException("失败",201);
//        }

        Apartment apartment = apartmentService.getById(id);
        return Result.ok(apartment);
    }
    //修改公寓设置
    @PostMapping("updateApartment")
    public Result updateApartment(@RequestBody Apartment apartment){
        boolean flag = apartmentService.updateById(apartment);
        if (flag) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //批量删除公寓设置

    @DeleteMapping("batchRemove")
    public Result batchRemoveApartment(@RequestBody List<Long> idList){
        apartmentService.removeByIds(idList);
        return Result.ok();
    }

    //公寓设置锁定和解锁
    @PutMapping("lockApartment/{id}/{status}")
    public Result lockApartment(@PathVariable Long id,
                                @PathVariable Integer status){
        //根据id查询公寓
        Apartment apartment = apartmentService.getById(id);
        //设置状态
        apartment.setStatus(status);
        //调用
        apartmentService.updateById(apartment);
        return Result.ok();
    }
    //发送签名key
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        Apartment apartment = apartmentService.getById(id);
        apartment.getSignKey();
        apartment.getApartmentCode();
        //TODO 发送短信
        return  Result.ok();
    }
}
